/**
 * @filename:IpadUserController Mar 4, 2024
 * @project Wonder Vue Admin Api  1.1.0
 * Copyright(c) 2020 Wonder Co. Ltd. 
 * All right reserved. 
 */
package com.wonder.app.controller;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Date;
import java.util.Map;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wonder.app.domain.IpadUser;
import com.wonder.app.domain.PrdTaskQty;
import com.wonder.app.domain.Synbd;
import com.wonder.app.mapper.SynbdDao;
import com.wonder.app.request.PrdTaskQtyRequest;
import com.wonder.app.service.IpadUserService;
import com.wonder.app.request.IpadUserRequest;
import com.wonder.app.service.KingdeeBasicService;
import com.wonder.common.exception.CustomException;
import com.wonder.common.utils.DateUtils;
import com.wonder.common.utils.file.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.wonder.common.common.AjaxMap;
import com.wonder.common.common.AjaxResult;
import com.wonder.common.common.CommonPage;
import com.wonder.common.utils.MessageUtils;
import com.wonder.common.utils.StringUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
/**   
 * 
 * <p>说明： 用户信息API接口层</P>
 * @version: 1.1.0
 * @author: Wonder
 * @time    Mar 4, 2024
 *
 */
@Api(tags = "用户信息")
@RestController
@RequestMapping("/app/IpadUser")
public class IpadUserController {

	@Autowired
	private SynbdDao synbdDao;

	@Autowired
	private KingdeeBasicService kingdeeBasicService;
	
	@Autowired
	private IpadUserService ipadUserService;
	
	@ApiOperation(value = "用户信息列表")
	@RequestMapping(value = "/index", method = RequestMethod.POST)
	public Map<String,Object> index(@RequestBody IpadUserRequest ipadUserRequest) {
		Page<IpadUser> page = new Page<IpadUser>(ipadUserRequest.getPage(), ipadUserRequest.getLimit());
		IPage<IpadUser> list = ipadUserService.selectList(ipadUserRequest, page);
		
		Map<String,Object> retMap=new HashMap<>();
		retMap.put("data",CommonPage.restPage(list));
		
  
		return AjaxMap.success(retMap);
	}
	
	/**
	 * @explain 修改排序开关
	 * @param   entity
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/updateExt")
	@ApiOperation(value = "修改排序开关", notes = "作者：Wonder")
	public AjaxResult<IpadUser> updateExt(@RequestBody IpadUser entity){
		if (null!=entity) {
			boolean rsg = ipadUserService.updateById(entity);
			if (rsg) {
				  return AjaxResult.success("system.updateSuccess");
			  }else {
				  return AjaxResult.failed("system.updateFailed");
			  }
		}else {
			return AjaxResult.failed("system.parameterError");
		}
	}
	
	/**
	 * @explain 添加
	 * @param   entity
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/add")
	@ApiOperation(value = "添加", notes = "作者：Wonder")
	public AjaxResult<IpadUser> add(@RequestBody IpadUser entity){

		if (null!=entity) {
		    
		    String validate=ipadUserService.validate(entity,"new");
		    if(StringUtils.hasText(validate))
		         return AjaxResult.failed(validate);
			String feature = entity.getFeature();
			//根据地址获取图片的二进制数据
//			byte[] fileStream = FileUtils.getFileStream(feature);
//			entity.setImageBinary(fileStream);
			boolean rsg = ipadUserService.save(entity);
			if (rsg) {
				  return AjaxResult.success("system.insertSuccess");
			  }else {
				  return AjaxResult.failed("system.insertFailed");
			  }
		}else {
			return AjaxResult.failed("system.parameterError");
		}
	}
	
	/**
	 * @explain 查看对象明细  
	 * @param   map 对象参数：id
	 * @return  JsonResult
	 * @author  Wonder
	 */
	@PostMapping("/detail")
	@ApiOperation(value = "获取用户信息对象明细", notes = "作者：Wonder")
	public AjaxResult<IpadUser> detail(@RequestBody Map<String,Object> map){
	    if(map==null) return AjaxResult.failed("system.parameterError");
		Long id=Long.parseLong(map.get("id").toString());		
		IpadUser ipadUser=ipadUserService.getById(id);
		if (null!=ipadUser ) {	
			 return AjaxResult.success(ipadUser);
		}else {
			 return AjaxResult.failed("system.parameterError");
		}
		
	}
	
	/**
	 * @explain 查询对象  <swagger GET请求>
	 * @param   map 对象参数：id
	 * @return  JsonResult
	 * @author  Wonder
	 */
	@PostMapping("/getUpdateInfo")
	@ApiOperation(value = "获取用户信息对象", notes = "作者：Wonder")
	public AjaxResult<IpadUser> getUpdateInfo(@RequestBody Map<String,Object> map){
	    if(map==null) return AjaxResult.failed("system.parameterError");
		Long id=Long.parseLong(map.get("id").toString());		
		 IpadUser ipadUser=ipadUserService.getById(id);
		
		if (null!=ipadUser ) {
			 return AjaxResult.success(ipadUser);
		}else {
			 return AjaxResult.failed("system.parameterError");
		}
		
	}
	
	/**
	 * @explain 修改
	 * @param   entity
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/update")
	@ApiOperation(value = "修改", notes = "作者：Wonder")
	public AjaxResult<IpadUser> update(@RequestBody IpadUser entity){
		if (null!=entity) {
		    String validate=ipadUserService.validate(entity,"update");
		    if(StringUtils.hasText(validate))
		         return AjaxResult.failed(validate);
			boolean rsg = ipadUserService.updateById(entity);
			if (rsg) {
				  return AjaxResult.success("system.updateSuccess");
			  }else {
				  return AjaxResult.failed("system.updateFailed");
			  }
		}else {
			return AjaxResult.failed("system.parameterError");
		}
	}

	/**
	 * 员工工资报表
	 * @param ipadUserRequest
	 * @return
	 */
	@ApiOperation(value = "员工工资报表")
	@RequestMapping(value = "/get_wages_list", method = RequestMethod.POST)
	public Map<String,Object> getWagesList(@RequestBody IpadUserRequest ipadUserRequest) throws Exception {
		Page<IpadUser> page = new Page<IpadUser>(ipadUserRequest.getPage(), ipadUserRequest.getLimit());
		Map<String,Object> data = ipadUserService.getUserWages(page,ipadUserRequest);
		if(null != data){
			return AjaxMap.success(data);
		} else {
			return AjaxMap.failed("查询失败");
		}


	}
	/**
	 * 同步员工信息
	 * @param
	 * @return
	 */
	@ApiOperation(value = "同步员工信息")
	@RequestMapping(value = "/sync_user", method = RequestMethod.POST)
	public AjaxResult<?> syncUser() throws Exception{
		//获取最后一次同步的数据
		QueryWrapper<Synbd> queryWrappersyn = new QueryWrapper<>();
		queryWrappersyn.eq("reloadRes",true);
		queryWrappersyn.eq("Ftype","员工");
		queryWrappersyn.orderByDesc("Fsyndate");
		queryWrappersyn.last("Limit 1");
		Synbd lastSynbd = synbdDao.selectOne(queryWrappersyn);
		//最后一次同步时间
//		Date fsyndate = lastSynbd.getFsyndate();
//		String endTime = DateUtils.dateToStr(fsyndate, "yyyy-MM-dd HH:mm:ss");
		//开始同步之前创建一个新的同步数据
		Synbd synbd = new Synbd();
		synbd.setReloadStartTime(new Date());
		synbd.setFtype("员工");
		try {
			kingdeeBasicService.syncUser("endTime");
			//同步结束修改同步状态
			synbd.setReloadEndTime(new DateTime());
			synbd.setFsyndate(new DateTime());
			synbd.setReloadRes(true);
			synbd.setReloadOrder(false);
			synbd.setReloadMsg("");
			synbdDao.insert(synbd);
			return AjaxResult.success();
		} catch (Exception e){
			synbd.setReloadEndTime(new DateTime());
			synbd.setReloadMsg(e.getMessage());
			synbd.setReloadRes(false);
			synbd.setReloadOrder(false);
			synbdDao.insert(synbd);
			throw new CustomException(e.getMessage());
		}
	}

	/**
	 * @explain 导出工资表
	 * @param   对象参数：Logininfor
	 * @return  Map<String,Object>
	 * @author  Wonder
	 */
	@PostMapping("/dumpdata")
	@ApiOperation(value = "导出数据", notes = "作者：Wonder")
	public Map<String,Object> dumpdata(@RequestBody IpadUserRequest ipadUserRequest) {
		return ipadUserService.dumpdata(ipadUserRequest);
	}
}